/*
 * Copyright (C) 2012 Jesse Webb (gweebz@gmail.com)
 *
 * This file is part of Gwahtzee.
 *
 * Gwahtzee is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gwahtzee is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gwahtzee.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.gwahtzee.gui.playgame;

import com.googlecode.gwahtzee.model.ScoreSheet;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.util.Log;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ScoreSheetGraphic {
    private int x;
    private int y;

    private Image image;
    private Map<ScoreSheet.Cell, Rectangle> coordinates;

    public ScoreSheetGraphic(int x, int y, Image image) {
        this.x = x;
        this.y = y;
        this.image = image;

        initCoordinates();
    }

    public boolean isMouseOverCell(int mouseX, int mouseY, ScoreSheet.Cell cell) {
        boolean isMouseOver = false;
        Rectangle cellRect = coordinates.get(cell);
        if (mouseX >= cellRect.getX() && mouseX <= cellRect.getX() + cellRect.getWidth() &&
                mouseY >= cellRect.getY() && mouseY <= cellRect.getY() + cellRect.getHeight()) {
            isMouseOver = true;
        }
        return isMouseOver;
    }

    public void renderHighlightedCell(Graphics graphics, ScoreSheet.Cell cell) {
        Rectangle rect = coordinates.get(cell);
        graphics.setColor(Color.magenta);
        graphics.drawRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());

        Integer myScore = 42;
//        java.awt.Font awtFont = new java.awt.Font(java.awt.Font.MONOSPACED, java.awt.Font.CENTER_BASELINE, 24);
//        Font font = new UnicodeFont(awtFont);
//        Log.debug("Loaded font: " + font.toString());
//        font.drawString(rect.getX(), rect.getY(), myScore.toString(), Color.black);
        graphics.setColor(Color.green);
        graphics.drawString(myScore.toString(), rect.getX() + 20, rect.getY() + 10);
    }

    public void update() {
    }

    public void render() {
        image.draw(x, y);
    }

    private void initCoordinates() {
        Log.debug(String.format("Loading ScoreSheetGraphic.coordinates"));
        coordinates = new HashMap<ScoreSheet.Cell, Rectangle>();
        String filename = "coordinates/score-sheet.properties";
        try {
            Properties coordinatesProperties = new Properties();
            InputStream coordinatesStream = ClassLoader.getSystemResourceAsStream(filename);
            coordinatesProperties.load(coordinatesStream);

            for (ScoreSheet.Cell cell : ScoreSheet.Cells) {
                String[] xYWidthHeight = coordinatesProperties.getProperty(cell.toString()).split(",");
                Rectangle rectangle = new Rectangle(Integer.parseInt(xYWidthHeight[0]) + x, Integer.parseInt(xYWidthHeight[1]) + y,
                        Integer.parseInt(xYWidthHeight[2]), Integer.parseInt(xYWidthHeight[3]));
                coordinates.put(cell, rectangle);
            }

        } catch (Exception e) {
            throw new RuntimeException(String.format("Failed to load score sheet coordinates from '%s'", filename), e);
        }
    }

    private static class Rectangle {
        private int x;
        private int y;
        private int width;
        private int height;

        private Rectangle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }
    }
}
